﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using NetJsWire.Lite.Widgets.UI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace NetJsWire.Lite.Converters
{
    public class BaseComponentConverter : JsonConverter
    {
        #region Overrides of JsonConverter

        /// <summary>
        ///   Writes the JSON representation of the object.
        /// </summary>
        /// <param name = "writer">The <see cref = "T:Newtonsoft.Json.JsonWriter" /> to write to.</param>
        /// <param name = "value">The value.</param>
        /// <param name = "serializer">The calling serializer.</param>
        public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
        {
            try
            {
                //var comp = value as BaseComponent;
                if ( value != null )
                {
                    //var converter = serializer.Converters[ 0 ];
                    //serializer.Converters.Remove( converter );
                    //if ( comp.ShouldSerializeExtraProperties() )
                    //{
                    //    var ser = JsonSerializer.Create( new JsonSerializerSettings
                    //    {
                    //        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    //        DefaultValueHandling = DefaultValueHandling.Ignore,
                    //        NullValueHandling = NullValueHandling.Ignore,
                    //        Converters =
                    //            new JsonConverter[]
                    //                                                {
                    //                                                    new ListenerCollectionJsonConverter(),
                    //                                                    new ColumnModelJsonConverter(),
                    //                                                    new GridViewJsonConverter(),
                    //                                                    new DataProxyJsonConverter(),
                    //                                                    new DataReaderJsonConverter(),
                    //                                                    new DataWriterJsonConverter(),
                    //                                                    new CheckBoxSelectionModelColumnJsonConverter()
                    //                                                }
                    //    } );
                    //    var jobj = JObject.FromObject( value, ser );
                    //    var obj = value as BaseComponent;
                    //    if ( obj != null && obj.ShouldSerializeExtraProperties() )
                    //    {
                    //        if ( obj.ExtraProperties != null )
                    //        {
                    //            foreach ( var property in obj.ExtraProperties )
                    //            {
                    //                jobj[ property.Name ] = property.Value;
                    //            }
                    //        }
                    //    }
                    //    foreach ( var property in jobj.Properties() )
                    //    {
                    //        //writer.WritePropertyName( property.Name );
                    //        property.WriteTo( writer, serializer.Converters.ToArray() );
                    //        //serializer.Serialize( writer, property );// writer.WriteValue( property.Value );
                    //    }

                    //    //writer.WriteRaw( jobj.ToString() );
                    //}
                    //else
                    //{
                    //    //serializer.Converters.Insert( 0, converter );
                    //    serializer.Serialize( writer, comp );
                    //}
                    //if ( !serializer.Converters.Contains( converter ) )
                    //    serializer.Converters.Insert( 0, converter );
                }
            }
            catch ( Exception e )
            {
                MessageBox.Show( e.StackTrace );
                throw;
            }
        }

        /// <summary>
        ///   Reads the JSON representation of the object.
        /// </summary>
        /// <param name = "reader">The <see cref = "T:Newtonsoft.Json.JsonReader" /> to read from.</param>
        /// <param name = "objectType">Type of the object.</param>
        /// <param name = "existingValue">The existing value of object being read.</param>
        /// <param name = "serializer">The calling serializer.</param>
        /// <returns>
        ///   The object value.
        /// </returns>
        public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///   Determines whether this instance can convert the specified object type.
        /// </summary>
        /// <param name = "objectType">Type of the object.</param>
        /// <returns>
        ///   <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
        /// </returns>
        public override bool CanConvert( Type objectType )
        {
            return typeof( BaseComponent ).IsAssignableFrom( objectType );
        }

        #endregion
    }
}
